response = Map();
info answers;
//Context handler for ticket creation
if(context_id.equals("createticket"))
{
	response.put("action","context");
	response.put("context_id","endaction");
	name = answers.get("name").get("text");
	email = answers.get("email").get("text");
	issue = answers.get("issue").get("text");
	/***Desk integration:
	- If the desk portal belongs to the salesIQ admin, 
		zoho.support.create("Requests",portal-name,department-name,data-map)
	- If the desk portal doesnot belong to the salesIQ admin, mention the connection name
		zoho.support.create("Requests",portal-name,department-name,data-map,True,connection-name)
	***/
	ticketInfo = zoho.support.create("Requests","rebootcorp","rebootcorp",{"Contact Name":name,"Subject":"Customer Issue-" + name,"Description":issue,"Email":email});
	info ticketInfo;
	record = zoho.support.getRecordById("Requests","rebootcorp","rebootcorp",ticketInfo.get("id"));
	ticketNumber = record.get("Request Id");
	response.put("replies",{"A ticket has been raised for your request. Our support team will resolve the issue at the earliest. Your ticket reference Id:" + ticketNumber});
	endaction = {"name":"endaction","replies":{"A ticket has been raised for your request. Our support team will resolve the issue at the earliest. Your ticket reference Id:" + ticketNumber,"Is there anything else I can help you with?"},"input":{"type":"select","options":{"Go to support menu","Go to main menu","End chat"}}};
	response.put("questions",{endaction});
}
//Context handler for checking a ticket status
else if(context_id.equals("checkticket"))
{
	response.put("action","context");
	response.put("context_id","endaction");
	email = answers.get("email").get("text");
	requestId = answers.get("requestId").get("text");
	/***Desk integration:
	- If the desk portal belongs to the salesIQ admin, 
		zoho.support.getRecords("Requests",portal-name,department-name)
	- If the desk portal doesnot belong to the salesIQ admin, mention the connection name
		zoho.support.getRecords("Requests",portal-name,department-name,0,0,"",connection-name)
	***/
	records = zoho.support.getRecords("Requests","rebootcorp","rebootcorp");
	finalOutput = "";
	info records;
	if(!records.isEmpty())
	{
		finalOutput = "Sorry, No tickets were created from the given email id";
		for each  entries in records
		{
			jsonemailId = entries.get("Email");
			if(jsonemailId.equals(email))
			{
				finalOutput = "Sorry, Ticket Id doesnot exist for the given email";
				jsonrequestId = entries.get("Request Id");
				if(jsonrequestId.equals(requestId))
				{
					status = entries.get("Status");
					finalOutput = "Your ticket is " + status;
					endaction = {"name":"endaction","replies":{finalOutput,"Is there anything else I can help you with?"},"input":{"type":"select","options":{"Go to support menu","Go to main menu","End chat"}}};
					response.put("questions",{endaction});
					return response;
				}
			}
		}
	}
	else
	{
		finalOutput = "Sorry. No records exist";
	}
	endaction = {"name":"endaction","replies":{finalOutput,"Is there anything else I can help you with?"},"input":{"type":"select","options":{"Go to support menu","Go to main menu","End chat"}}};
	response.put("questions",{endaction});
}
//Context handler for searching suitable product
else if(context_id.equals("selectsuitableproduct"))
{
	response.put("action","context");
	response.put("context_id","endaction");
	category = answers.get("category").get("text");
	if(category.equalsIgnoreCase("Fashion"))
	{
		endaction = {"name":"productselection","replies":{{"text":"For Fashion we have","type":"links","links":{{"url":"http://zylker-fashions.weebly.com","text":"Zylker Fashion"}}},"Hope you found the product you needed."},"input":{"type":"select","options":{"Go to products menu","Go to main menu","End chat"}}};
	}
	else if(category.equalsIgnoreCase("Shoes"))
	{
		endaction = {"name":"productselection","replies":{{"text":"For Shoes we have","type":"links","links":{{"url":"http://zylker.shoes.weebly.com","text":"Zylker Shoes"}}},"Hope you found the product you needed."},"input":{"type":"select","options":{"Go to products menu","Go to main menu","End chat"}}};
	}
	else if(category.equalsIgnoreCase("Furniture"))
	{
		endaction = {"name":"productselection","replies":{{"text":"For Furniture we have","type":"links","links":{{"url":"http://zylker-furns.weebly.com","text":"Zylker Furnitures"}}},"Hope you found the product you needed."},"input":{"type":"select","options":{"Go to products menu","Go to main menu","End chat"}}};
	}
	else if(category.equalsIgnoreCase("Techs"))
	{
		endaction = {"name":"productselection","replies":{{"text":"For Techs we have","type":"links","links":{{"url":"https://zylkertechnologies.weebly.com","text":"Zylker Technologies"}}},"Hope you found the product you needed."},"input":{"type":"select","options":{"Go to products menu","Go to main menu","End chat"}}};
	}
	else if(category.equalsIgnoreCase("Design"))
	{
		endaction = {"name":"productselection","replies":{{"text":"For design we have","type":"links","links":{{"url":"https://zylker-inc.weebly.com","text":"Zylker Design"}}},"Hope you found the product you needed."},"input":{"type":"select","options":{"Go to products menu","Go to main menu","End chat"}}};
	}
	response.put("questions",{endaction});
}
//Context handler for scheduling a call
else if(context_id.equals("schedulecall"))
{
	response.put("action","context");
	response.put("context_id","endaction");
	date = answers.get("date");
	meta = date.get("meta");
	card_data = meta.get("card_data");
	slot = card_data.get("value").get("slot");
	formattedDate = slot.toList("/");
	slot = formattedDate.get(1) + "/" + formattedDate.get(0) + "/" + formattedDate.get(2);
	value = slot.toDateTime();

	name = answers.get("name").get("text");
	phone = answers.get("phone").get("text");
	email = answers.get("email").get("text");
	selectedSlot = value.getYear().toString() + "-" + value.getMonth().toString() + "-" + value.getDay().toString() + "T" + value.getHour().toString() + ":" + value.getMinutes().toString() + ":" + value.getSeconds().toString();
	//indian time zone has been configured
	timeString = {"start":{"dateTime":selectedSlot,"timeZone":"Asia/Kolkata"},"end":{"dateTime":selectedSlot,"timeZone":"Asia/Kolkata"},'summary':'Call with ' + name + '. Contact Number : ' + phone.toString()};
	/***Google calendar integration:
	- Events are created in the primary calendar of the google account 
	- To connect a different google calendar, replace the connection name		
		connection:"your-google-calendar-connection-name-goes-here"
	***/
	apiresponse = invokeurl
	[
		url :"https://www.googleapis.com/calendar/v3/calendars/primary/events"
		type :POST
		parameters:timeString.toString()
		headers:{'Content-type':'application/json;'}
		connection:"googlecalendar5"
	];
	info apiresponse;
	if(apiresponse.containsKey("status"))
	{
		status = apiresponse.get("status");
		if(status.equalsIgnoreCase("confirmed"))
		{
			confirmation = "Thank you.Your appointment has been scheduled with our Sales rep on " + slot;
		}
		else
		{
			confirmation = "Sorry, there seems to be an issue. please try again later";
		}
	}
	else
	{
		confirmation = "Sorry, there seems to be an issue. please try again later";
	}
	endaction = {"name":"endaction","replies":{confirmation,"Is there anything else I can help you with?"},"input":{"type":"select","options":{"Go to main menu","End chat"}}};
	response.put("questions",{endaction});
}
//get notified
else if(context_id.equals("getnotified"))
{
	if(answers.containsKey("getnotifiedafterproductselection") && answers.get("getnotifiedafterproductselection").get("text").equalsIgnoreCase("No thanks"))
	{
		response.put("action","context");
		response.put("context_id","endchat");
		rating = {"name":"rating","replies":{"Please rate the conversation"},"input":{"type":"star-rating","level":"5"}};
		response.put("questions",{rating});
	}
	else if(answers.containsKey("getnotified") && answers.get("getnotified").get("text").equalsIgnoreCase("No thanks"))
	{
		response.put("action","end");
		response.put("replies",{"Fine","Have a great day :)"});
		return response;
	}
	else
	{
		response.put("action","context");
		response.put("context_id","createlead");
		name = {"name":"name","replies":{"Excellent","I just need some info","To start with, Your name please ?"}};
		email = {"name":"email","replies":{"And your email id ?"}};
		leadDetails = Collection();
		leadDetails.insert(name);
		leadDetails.insert(email);
		response.put("questions",leadDetails);
	}
}
//Context handler for creating a lead
else if(context_id.equals("createlead"))
{
	name = answers.get("name").get("text");
	email = answers.get("email").get("text");
	//Search lead
	/***CRM Integration:
	- To connect with your Zoho crm, replace the connection name
		zoho.crm.searchRecords("Leads","(Email:equals:" + email + ")",0,0,Map(),"your-zoho-auth-connection-name-goes-here");
***/
	crmdata = zoho.crm.searchRecords("Leads","(Email:equals:" + email + ")",0,0,Map(),"zohoauth111");
	info crmdata;
	if(!crmdata.isEmpty())
	{
		// update existing lead
		/***CRM Integration:
	- To connect with your Zoho crm, replace the connection name
		crmdata = zoho.crm.update("Leads",leadid,{"Last_Name":name},Map(),"your-zoho-auth-connection-name-goes-here");
***/
		_crmdata = crmdata.get(0);
		info _crmdata;
		leadid = _crmdata.get("id");
		crmdata = zoho.crm.update("Leads",leadid,{"Last_Name":name},Map(),"zohoauth111");
	}
	else
	{
		//Create new lead	
		/***CRM Integration:
	- To connect with your Zoho crm, replace the connection name
		zoho.crm.create("Leads",{"Email":email,"Last_Name":name},Map(),"your-zoho-auth-connection-name-goes-here");
***/
		crmdata = zoho.crm.create("Leads",{"Email":email,"Last_Name":name},Map(),"zohoauth111");
	}
	response.put("action","context");
	response.put("context_id","endchat");
	rating = {"name":"rating","replies":{"Great. You will now get to know all our new products and upgrades as and when they are released","Please rate the conversation"},"input":{"type":"star-rating","level":"5"}};
	response.put("questions",{rating});
}
//Context handler for getting feedback or navigating back to mainmenu
else if(context_id.equals("endaction"))
{
	if(answers.containsKey("productselection"))
	{
		productSelectionEnd = answers.get("productselection").get("text");
		if(productSelectionEnd.equalsIgnoreCase("End chat"))
		{
			response.put("action","context");
			response.put("context_id","getnotified");
			getnotifiedafterproductselection = {"name":"getnotifiedafterproductselection","replies":{"Do you want to get notified about our latest products and upgrades?"},"input":{"type":"select","options":{"Yes","No thanks"}}};
			response.put("questions",{getnotifiedafterproductselection});
		}
		else if(productSelectionEnd.equalsIgnoreCase("Go to main menu"))
		{
			response.put("action","reply");
			response.put("replies",{"What would you like to do"});
			response.put("input",{"type":"select","options":{"Buy a product","Need support","Just browsing"}});
		}
		else if(productSelectionEnd.equalsIgnoreCase("Go to products menu"))
		{
			response.put("action","context");
			response.put("context_id","selectsuitableproduct");
			category = {"name":"category","replies":{"We have an amazing collection of products and I'm sure I can show the right one for your needs.","What kind of product are you looking for?"},"input":{"type":"select","options":{"Design","Fashion","Furniture","Techs","Shoes"}}};
			response.put("questions",{category});
		}
	}
	else
	{
		endaction = answers.get("endaction").get("text");
		if(endaction.equalsIgnoreCase("End chat"))
		{
			response.put("action","context");
			response.put("context_id","endchat");
			rating = {"name":"rating","replies":{"please rate the conversation"},"input":{"type":"star-rating","level":"5"}};
			response.put("questions",{rating});
		}
		else if(endaction.equalsIgnoreCase("Go to main menu"))
		{
			response.put("action","reply");
			response.put("replies",{"What would you like to do"});
			response.put("input",{"type":"select","options":{"Buy a product","Need support","Just browsing"}});
		}
		else if(endaction.equalsIgnoreCase("Go to support menu"))
		{
			response.put("action","reply");
			response.put("replies",{"How can we assist you"});
			response.put("input",{"type":"select","options":{"Raise a ticket","Check my ticket status"}});
		}
	}
}
//Context handler for closing the chat
else if(context_id.equals("endchat"))
{
	response.put("action","end");
	response.put("replies",{"Thank you. Have a great day"});
}
return response;